//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Configuration;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;
using CommunityServer.Mail.Common.Components;

namespace CommunityServer.Discussions.Controls
{
	/// <summary>
	/// Summary description for NntpInformation.
	/// </summary>
	public class EmailInformation : TemplatedWebControl
	{
		CSContext csContext = CSContext.Current;
		
		CheckBox IsSubscribed;
		IButton SaveButton;
		IText ForumEmailAddress;
		IText CurrentEmailAddress;
		StatusMessage StatusMessage;

		MailingList mailingList = null;
			
		protected override void AttachChildControls() 
		{
			IsSubscribed = FindControl("IsSubscribed") as CheckBox;
			SaveButton = FindButton("SaveButton");
			ForumEmailAddress = FindText("ForumEmailAddress");
			CurrentEmailAddress = FindText("CurrentEmailAddress");
			StatusMessage = FindControl("StatusMessage") as StatusMessage;

			if (SaveButton != null)
			{
				SaveButton.Click += new EventHandler(SaveButton_Click);
				SaveButton.Text = ResourceManager.GetString("Save");
			}
		}

		protected override void OnLoad(EventArgs e) 
		{
			if (!csContext.SiteSettings.EnableMailGateway)
				throw new CSException(CSExceptionType.AccessDenied, "Mail Gateway is not enabled for this site");

			if (csContext.User.IsAnonymous)
			{
				this.Visible = false;
				return;
			}

			Head.AddSiteNameTitle(ResourceManager.GetString("EmailInformation_Title"), Context);
			UsersOnline.SetLocation(ResourceManager.GetString("EmailInformation_Title"));

			mailingList = MailingLists.GetMailingList(csContext.SectionID);
			if (mailingList == null || !mailingList.IsActive || !mailingList.IsMailingList)
				throw new CSException(CSExceptionType.AccessDenied, "The specified forum is not a mailing list");

			this.DataBind();
			base.OnLoad (e);
		}

		public override void DataBind() 
		{
			base.DataBind();

			if (ForumEmailAddress != null)
				ForumEmailAddress.Text = string.Format(ResourceManager.GetString("EmailInformation_ForumEmailAddress"), mailingList.DetermineEmailAddress());

			if (CurrentEmailAddress != null)
				CurrentEmailAddress.Text = string.Format(ResourceManager.GetString("EmailInformation_CurrentEmailAddress"), csContext.User.Email, SiteUrls.Instance().UserEditProfile, ForumUrls.Instance().ForumSubscriptions);

			if (!Page.IsPostBack && IsSubscribed != null)
				IsSubscribed.Checked = (ThreadTracking.GetSectionSubscriptionType(csContext.SectionID, csContext.User.UserID) & EmailSubscriptionType.Thread) > 0;
		}

		private void SaveButton_Click(object sender, EventArgs e)
		{
			if (IsSubscribed != null)
			{
				EmailSubscriptionType current = ThreadTracking.GetSectionSubscriptionType(csContext.SectionID, csContext.User.UserID);
				if (IsSubscribed.Checked)
				{
					if ((current & EmailSubscriptionType.Thread) == 0)
						ThreadTracking.SetSectionSubscriptionType(csContext.SectionID, csContext.User.UserID, current | EmailSubscriptionType.Thread);
				}
				else if ((current & EmailSubscriptionType.Thread) > 0)
					ThreadTracking.SetSectionSubscriptionType(csContext.SectionID, csContext.User.UserID, (EmailSubscriptionType) (current - EmailSubscriptionType.Thread));

				if (StatusMessage != null)
				{
					StatusMessage.Text = ResourceManager.GetString("EmailInformation_SaveSuccessful");
					StatusMessage.Success = true;
					StatusMessage.Visible = true;
				}
			}
		}
	}
}
